﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
namespace Skynet.WebSite.Demos
{
    public class LoaderHttpModule :IHttpModule
    {
        public void Dispose()
        {
         
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
            
            
        }

        void context_BeginRequest(object sender, EventArgs e)
        {

            string absPath = HttpContext.Current.Request.Url.AbsolutePath;
            var resource = System.IO.Path.GetFileName(absPath);
            if (resource.ToLower() == "HttpModuleLoader.aspx".ToLower())
            {
                var str = System.Configuration.ConfigurationManager.ConnectionStrings["SkynetLibrary"].ConnectionString;
                var con = new SqlConnection(str);
                var cmd = con.CreateCommand();
                cmd.CommandText = "usp_busyprocess";
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@delay", "00:00:05");
                con.Open();
                var result = cmd.BeginExecuteReader(System.Data.CommandBehavior.SingleResult | System.Data.CommandBehavior.CloseConnection);
                HttpContext.Current.Items["usp_busyprocess"] = result;
                HttpContext.Current.Items["usp_busyprocess_command"] = cmd;
            }
        }        
    }
}